package com.patrick.open.api.core.security;

import com.patrick.open.api.core.event.RemoteRefreshGatewayEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;

/**
 * @author patrick
 * @date 2020/6/1 10:31 下午
 * @Des 刷新网关
 * 最簡單的事是堅持，最難的事還是堅持
 */
@Slf4j
public class OpenRefreshGateway {

  private ApplicationEventPublisher publisher;
  private BusProperties busProperties;

  public OpenRefreshGateway(BusProperties busProperties, ApplicationEventPublisher publisher) {
    this.publisher = publisher;
    this.busProperties = busProperties;
  }

  /**
   * 刷新网关
   * 注:不要频繁调用!
   * 1.资源权限发生变化时可以调用
   * 2.流量限制变化时可以调用
   * 3.IP访问发生变化时可以调用
   * 4.智能路由发生变化时可以调用
   */
  public void refreshGateway() {
    try {
      publisher.publishEvent(new RemoteRefreshGatewayEvent(this, busProperties.getId(), null));
      log.info("refreshGateway:success");
    } catch (Exception e) {
      log.error("refreshGateway error:{}", e.getMessage());
    }
  }

}
